﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace WebSite_CodeMan.Sys_CodeMan_Manage
{
    public partial class Admin_Create_Html : System.Web.UI.Page
    {
        protected string[] strMes = new string[5];
        protected void Page_Load(object sender, EventArgs e)
        {
            BaseClass.BaseClass.CheckState();
            Common.MessageBox.myScriptMes(Page, "CloseDiv()", "Load");
            if (!IsPostBack)
            {
                BaseClass.BaseClass.GetNoteClassMes(DDLClass);
            }
        }
        
        /// <summary>
        /// 生成首页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnHome_Click(object sender, EventArgs e)
        {
            try
            {
                GenerationHomePage();
                //int pageCount = 1;
                //int recordCount = 0;
                //StringBuilder strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("", "", Page);
                //strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, "", "");
                //strHtml.Replace("{#WebHead}", BaseClass.HtmlCommon.HtmlReplaceHead(Page, "").ToString());
                //strHtml.Replace("{#LeftNote}", BaseClass.HtmlCommon.HtmlReplaceList(Page, "", out pageCount, out recordCount, 1,false).ToString());
                ////strHtml.Replace("{#RightNote}", BaseClass.HtmlCommon.HtmlGetTopRndNote().ToString() + BaseClass.HtmlCommon.GetTopTMes("10") + BaseClass.HtmlCommon.HtmlGetTopNewNote().ToString() + BaseClass.HtmlCommon.HtmlGetClassMes() + BaseClass.HtmlCommon.HtmlGetTopNote().ToString() + BaseClass.HtmlCommon.HtmlGetHotKey("20", Page).ToString() + BaseClass.HtmlCommon.HtmlSearch().ToString());
                //strHtml.Replace("{#RightNote}", BaseClass.HtmlCommon.HtmlGetTopRndNote().ToString() + BaseClass.HtmlCommon.HtmlGetTopNewNote().ToString() + BaseClass.HtmlCommon.HtmlGetClassMes() + BaseClass.HtmlCommon.HtmlGetTopNote().ToString() + BaseClass.HtmlCommon.HtmlGetHotKey("20", Page).ToString() + BaseClass.HtmlCommon.HtmlSearch().ToString());

                //strHtml.Replace("{#WebLink}", BaseClass.HtmlCommon.HtmlReplaceLink().ToString());
                //strHtml.Replace("{#WebFoot}", BaseClass.HtmlCommon.HtmlReplaceFoot(Page).ToString());
                //strHtml.Replace("{#About}", Common.GetMes.GetFirstString(Common.SetMes.NoHTML(BaseClass.HtmlCommon.HtmlAboutMes().ToString()), 700));
                //Common.FileComm.WriteHtml("~/Index.html", strHtml.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        /// <summary>
        /// 遍历{#XXXX|00}结构的元素 并替换<br/>
        /// 用于页面上自由指定的note类别，降低定制首页时的开发量<br/>
        /// XXXX限定在T_NOTE_CLASS表<br/>
        /// 步骤：<br/>
        /// 1.检索出T_NOTE_CLASS表里的所有classenname<br/>
        /// 2.正则找出{#XXXX}，并筛选掉不在T_NOTE_CLASS列表里的class<br/>
        /// 3.调用htmlcommon.GetNoteNoStyle<br/>
        /// 4.对应替换
        /// </summary>
        /// <returns></returns>
        private bool GenerationHomePage()
        {
            var dtNoteClass = BLL.NoteInfo.GetAllNoteClass();

            if (dtNoteClass.Rows.Count==0)
            {
                return false;
            }

            StringBuilder strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("", "", Page);
            strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, "", "");
            
            strHtml.Replace("{#WebHead}", BaseClass.HtmlCommon.HtmlReplaceHead(Page, "").ToString());
            strHtml.Replace("{#WebHotNote}", BaseClass.HtmlCommon.GetHotNote());
            //内容部分开始

            //定义正则，并找出页面上的{#xxxx|22|2}
            var regex = new Regex(@"\{\#[a-zA-Z_0-9]*\|[0-9]*\|[0-9]*\}", RegexOptions.IgnoreCase);
            var matchCollection = regex.Matches(strHtml.ToString());
            int matchCount = matchCollection.Count;

            if (matchCount <= 0) return false;

            //循环｛#XXXX｝列表 {#df9}
            foreach(Match match in matchCollection)
            {
                //分离出类别和需要显示的记录数
                var noteclassandtop = match.Value.Replace("{#", "").Replace("}", "");
                var index1 = noteclassandtop.IndexOf("|");
                var topAndType =noteclassandtop.Substring(index1 + 1);
                var noteclass = noteclassandtop.Substring(0, index1);
                var index2 = topAndType.IndexOf("|");
                var top = Convert.ToInt32(topAndType.Substring(0, index2));
                var type = Convert.ToInt32(topAndType.Substring(index2 + 1));

                for (int j = 0; j < dtNoteClass.Rows.Count; j++)
                {
                    //如果noteclass与数据库中的noteclass有匹配
                    if (noteclass.ToLower().Equals(dtNoteClass.Rows[j]["t_classenname"].ToString().ToLower() ))
                    {
                        strHtml.Replace("{#" + noteclassandtop + "}", BaseClass.HtmlCommon.GetNote(top, dtNoteClass.Rows[j]["t_classenname"].ToString(), type));
                    }
                }
            }

            

            //内容部分结束

            strHtml.Replace("{#WebLink}", BaseClass.HtmlCommon.HtmlReplaceLink().ToString());
            strHtml.Replace("{#WebFoot}", BaseClass.HtmlCommon.HtmlReplaceFoot(Page).ToString());
            strHtml.Replace("{#About}", Common.GetMes.GetFirstString(Common.SetMes.NoHTML(BaseClass.HtmlCommon.HtmlAboutMes().ToString()), 700));
            Common.FileComm.WriteHtml("~/Index.html", strHtml.ToString());

            return true;
        }
        /// <summary>
        /// 生成列表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnList_Click(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(DDLClass.SelectedValue))
                {
                    int pageCount = 1;
                    int recordCount = 0;
                    StringBuilder strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("list", DDLClass.SelectedValue, Page);
                    strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, DDLClass.SelectedValue, "list");
                    strHtml.Replace("{#WebHead}", BaseClass.HtmlCommon.HtmlReplaceHead(Page, DDLClass.SelectedValue).ToString());
                    strHtml.Replace("{#RightNote}", BaseClass.HtmlCommon.HtmlGetTopRndNote().ToString() + BaseClass.HtmlCommon.HtmlGetClassMes() + BaseClass.HtmlCommon.HtmlGetTopNewNote().ToString() + BaseClass.HtmlCommon.HtmlGetTopNote().ToString() + BaseClass.HtmlCommon.HtmlGetHotKey("20", Page).ToString() + BaseClass.HtmlCommon.HtmlSearch().ToString());
                    strHtml.Replace("{#WebFoot}", BaseClass.HtmlCommon.HtmlReplaceFoot(Page).ToString());
                    StringBuilder newHtml = new StringBuilder(strHtml.ToString());
                    strHtml.Replace("{#LeftNote}", BaseClass.HtmlCommon.HtmlReplaceList(Page, DDLClass.SelectedValue, out pageCount, out recordCount, 1, true).ToString()); strHtml.Replace("{#WebPage}", BaseClass.HtmlCommon.GetPageMes(pageCount, recordCount, 1).ToString());
                    Common.FileComm.WriteHtml("~/" + GetEnClassName() + "/Index.html", strHtml.ToString());
                    strHtml.Remove(0, strHtml.Length);
                    strHtml.Append(newHtml.ToString());
                    if (pageCount > 1)
                    {
                        for (int i = 2; i <= pageCount; i++)
                        {
                            strHtml.Replace("{#LeftNote}", BaseClass.HtmlCommon.HtmlReplaceList(Page, DDLClass.SelectedValue, out pageCount, out recordCount, i,true).ToString());
                            strHtml.Replace("{#WebPage}", BaseClass.HtmlCommon.GetPageMes(pageCount, recordCount, i).ToString());
                            Common.FileComm.WriteHtml("~/" + GetEnClassName() + "/Index_" + i + ".html", strHtml.ToString());
                            strHtml.Remove(0, strHtml.Length);
                            strHtml.Append(newHtml.ToString());
                        }
                    }
                }
                else
                {
                    Common.MessageBox.Show(Page, "未检测到选中的类别信息，请重新选择...", "errorlist", "Admin_Create_Html.aspx");
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// 截取EnName
        /// </summary>
        /// <returns></returns>
        private string GetEnClassName()
        {
            string strCutValue = DDLClass.SelectedItem.Text.Substring(DDLClass.SelectedItem.Text.IndexOf("[") + 1);
            return strCutValue.Replace("]", "");
        }

        /// <summary>
        /// 生成未生成的Html内容信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnNote_Click(object sender, EventArgs e)
        {
            try
            {
                int pageCount = 1;
                int recordCount = 0;
                //取一条 获取记录数量
                DataTable DataRead = BLL.NoteInfo.HtmlGetAllNoteInfo(1, 10, out pageCount, out recordCount);
                StringBuilder strHtml = new StringBuilder();
                StringBuilder newHtml = new StringBuilder();
                for (int i = 0; i < pageCount; i++)
                {
                    DataRead = BLL.NoteInfo.HtmlGetAllNoteInfo(i, 10, out pageCount, out recordCount);
                    for (int j = 0; j < DataRead.Rows.Count; j++)
                    {
                        if (!((HClassID.Value.Equals(DataRead.Rows[j]["T_NoteClassID"].ToString())) ? true : false))
                        {
                            strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("note", DataRead.Rows[j]["T_NoteClassID"].ToString(), Page);
                            strHtml.Replace("{#RightNote}", BaseClass.HtmlCommon.HtmlGetTopRndNote().ToString() + BaseClass.HtmlCommon.HtmlGetClassMes() + BaseClass.HtmlCommon.HtmlGetTopNote().ToString() + BaseClass.HtmlCommon.HtmlGetHotKey("40", Page).ToString() + BaseClass.HtmlCommon.HtmlGetTopNewNote().ToString() + BaseClass.HtmlCommon.HtmlSearch().ToString());
                            strHtml.Replace("{#WebFoot}", BaseClass.HtmlCommon.HtmlReplaceFoot(Page).ToString());
                            newHtml.Remove(0, newHtml.Length);
                            newHtml.Append(strHtml.ToString());
                        }
                        strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, DataRead.Rows[j]["T_NoteID"].ToString(), "note");
                        strHtml.Replace("{#NID}", DataRead.Rows[j]["T_NoteID"].ToString());
                        strHtml.Replace("{#WebHead}", BaseClass.HtmlCommon.HtmlReplaceHead(Page, DataRead.Rows[j]["T_NoteClassID"].ToString()).ToString());
                        strHtml.Replace("{#NoteTitle}", DataRead.Rows[j]["T_NoteTitle"].ToString());
                        strHtml.Replace("{#WebTitle}", DataRead.Rows[j]["T_NoteTitle"].ToString());
                        strHtml.Replace("{#NoteKey}", BaseClass.HtmlCommon.TagsKeyCommon(DataRead.Rows[j]["T_NoteKey"].ToString(), Page));
                        strHtml.Replace("{#NoteAbout}", "类别:<a href=../" + DataRead.Rows[j]["T_ClassEnName"].ToString() + "/Index.html target=\"_blank\" title=\"" + DataRead.Rows[j]["T_ClassName"].ToString() + "\">" + DataRead.Rows[j]["T_ClassName"].ToString() + "</a>  时间:" + Convert.ToDateTime(DataRead.Rows[j]["T_DateTime"].ToString()).ToString("yyyy年MM月dd日") + "  阅读:<span  style=\"color:#4AD228;\" class=\"ReadCount\">" + DataRead.Rows[j]["T_BrowerCount"].ToString() + "</span>  评论:<a  title=\"" + DataRead.Rows[j]["T_NoteTitle"].ToString() + "\" target=\"_blank\" class=\"LeaveCount\" href=\"../LeaveMes.aspx?NID=" + DataRead.Rows[j]["T_NoteID"].ToString() + "\" style=\"color:#4AD228;\">0</a>  <a  class=\"WantLeave\" href=\"javascript:void(0)\">↓说两句↓</a>");
                        strHtml.Replace("{#NextUp}", BaseClass.HtmlCommon.HtmlNextUp(DataRead.Rows[j]["T_NoteID"].ToString()).ToString());
                        PageNote(strHtml, Server.HtmlDecode(DataRead.Rows[j]["T_NoteMes"].ToString()), DataRead.Rows[j]["T_ClassEnName"].ToString(), DataRead.Rows[j]["T_NoteID"].ToString(), DataRead.Rows[j]["T_NoteImage"].ToString(), DataRead.Rows[j]["T_NoteDes"].ToString());
                        strHtml.Remove(0, strHtml.Length);
                        strHtml.Append(newHtml.ToString());
                        HClassID.Value = DataRead.Rows[j]["T_NoteClassID"].ToString();
                        HNID.Value += DataRead.Rows[j]["T_NoteID"].ToString() + ",";
                    }
                }
                if (!string.IsNullOrEmpty(HNID.Value))
                {
                    BLL.NoteInfo.UpdateHtmlState(HNID.Value.Substring(0, HNID.Value.Length), "1");
                }
                HNID.Value = string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="strHtml"></param>
        /// <param name="strEnName"></param>
        /// <param name="strNID"></param>
        /// <returns></returns>
        private void PageNote(StringBuilder HtmlNote, string strHtml, string strEnName, string strNID, string strTopImage,string strDes)
        {
            if (strHtml.Contains("{page}"))
            {
                StringBuilder newHtml = new StringBuilder(HtmlNote.ToString());
                string[] strNewHtml = strHtml.Replace("{page}", "€").Split('€');
                for (int i = 0; i < strNewHtml.Length; i++)
                {
                    HtmlNote.Replace("{#Note}", ((i != 0) ? strNewHtml[i].Replace("\r\n", "") : ((string.IsNullOrEmpty(strTopImage) || strTopImage == "noImage.gif") ? "" : "<p style=\"text-align: center; \"><img class=\"NoteTopImage\" src=\"../UpLoad/Images/" + strTopImage + "\" /></p><br/>") + strNewHtml[i].Replace("\r\n", "")) + "<br/>" + GetNotePageMes(i, strNewHtml.Length, strNID));
                    HtmlNote.Replace("{#Des}", ((i == 0) ? strDes + ".." : ""));
                    Common.FileComm.WriteHtml("~/" + strEnName + "/" + strNID + ((i == 0) ? "" : ("_" + i)) + ".html", HtmlNote.ToString());
                    HtmlNote.Remove(0, HtmlNote.Length);
                    HtmlNote.Append(newHtml.ToString());
                }
            }
            else
            {
                HtmlNote.Replace("{#Note}", ((string.IsNullOrEmpty(strTopImage) || strTopImage == "noImage.gif") ? "" : "<p style=\"text-align: center; \"><img class=\"NoteTopImage\" src=\"../UpLoad/Images/" + strTopImage + "\" /></p><br/>") + strHtml.Replace("\r\n", "")); 
                HtmlNote.Replace("{#Des}", strDes+"..");
                Common.FileComm.WriteHtml("~/" + strEnName + "/" + strNID + ".html", HtmlNote.ToString());
            }
        }
        /// <summary>
        /// 生成内容分页信息
        /// </summary>
        /// <param name="now"></param>
        /// <param name="lenght"></param>
        /// <returns></returns>
        private string GetNotePageMes(int nowPage, int pageCount, string strNID)
        {
            StringBuilder strHtml = new StringBuilder("<div class=\"manu NoteTitles\">");
            for (int i = 0; i < pageCount; i++)
            {
                strHtml.Append((nowPage == i) ? "<span class=\"current\">" + (i + 1) + "</span>" : "<a href=" + ((i == 0) ? strNID : (strNID + "_" + i)) + ".html >" + (i + 1) + "</a>");
            }
            return strHtml.Append("</div>").ToString();
        }

        protected void BtnBack_Click(object sender, EventArgs e)
        {
            BLL.NoteInfo.UpdateHtmlState("", "");
        }
        /// <summary>
        /// 个人简介
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAbout_Click(object sender, EventArgs e)
        {
            try
            {
                StringBuilder strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("about", "", Page);
                strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, "", "");
                strHtml.Replace("{#WebHead}", BaseClass.HtmlCommon.HtmlReplaceHead(Page, "about").ToString());
                strHtml.Replace("{#LeftNote}", Server.HtmlDecode(BaseClass.HtmlCommon.HtmlAboutMes().ToString()));
                strHtml.Replace("{#RightNote}", BaseClass.HtmlCommon.HtmlGetTopRndNote().ToString() +BaseClass.HtmlCommon.HtmlGetTopNote().ToString() + BaseClass.HtmlCommon.HtmlGetClassMes() + BaseClass.HtmlCommon.HtmlGetHotKey("40", Page).ToString() + BaseClass.HtmlCommon.HtmlSearch().ToString());

                strHtml.Replace("{#WebFoot}", BaseClass.HtmlCommon.HtmlReplaceFoot(Page).ToString());
                Common.FileComm.WriteHtml("~/About.html", strHtml.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void BtnSiteMap_Click(object sender, EventArgs e)
        {
            var strHtml = BaseClass.HtmlCommon.GetBaseHtmlNote("sitemap", "", Page);
            strHtml = BaseClass.HtmlCommon.HtmlReplaceWebMes(strHtml, "", "");
            var v = BaseClass.HtmlCommon.GenSiteMap();
            strHtml.Replace("{#sitemap}", v);
            Common.FileComm.WriteHtml("~/sitemap.html", strHtml.ToString());
        }
    }
}
